<?php

namespace app\index\service;

use app\common\model\Consulting;
use app\common\model\ConsultingMessage;
use app\index\dao\ConsultingDao;
use app\index\model\UM as UM;
use ary\Ary;
use ShidianUtil\Utils\CheckUtil;
use think\exception\ValidateException;
use think\facade\Config;

/**
 * Class ConsultingPhoneService
 * @package app\index\service
 */
class ConsultingPhoneService
{
    // dao
    private ConsultingDao $consultingDao;

    public function __construct(ConsultingDao $consultingDao)
    {
        $this->consultingDao = $consultingDao;
    }


    /**
     * 新增
     * @param UM $user
     * @param array $params
     * @return  array
     */
    public function bindAxbPhone(UM $user, $messageId, array $params)
    {
        $lawyerId = $user->id;
        $lawyerName = $user->showName;
        $lawyerPhone = $params['phone'] ?? $user->axbPhone;

        if (!is_id($messageId)) {
            throw new ValidateException('请选择一个消息');
        }

        $message = ConsultingMessage::where('id', $messageId)->find();
        if (is_null($message)) {
            throw new ValidateException('该消息不存或已被删除');
        }

        if ($message->messageType !== ConsultingMessage::PHONE) {
            throw new ValidateException('请选择一个电话消息卡片');
        }

        $content = $message->content;

        $memberPhone = $content->phone ?? '';
        if (!CheckUtil::isPhoneNumber($memberPhone)) {
            throw new ValidateException('未找到电话号码');
        }

        $consultingId = $message->consultingId;

        if (is_blank($consultingId)) {
            throw new ValidateException('请选择一个咨询');
        }

        $consulting = Consulting::where('id', $consultingId)->find();
        if (is_null($consulting)) {
            throw new ValidateException('该咨询不存或已被删除');
        }

        if ($consulting->lawyerId != $user->id) {
            throw new ValidateException('您未接单，不能拨打电话');
        }

        if ($consulting->rushStatus != 2) {
            throw new ValidateException('您还未确认接单，请确认后再拨打');
        }

        if ($consulting->isEnd == 1) {
            throw new ValidateException('该咨询已完成，无法拨打电话');
        }

        $data = [
            'user_id' => $lawyerId,
            'trade_id' => $consultingId,
            'call_mobile' => $lawyerPhone,
            'answer_mobile' => $memberPhone,
            'user_name' => $lawyerName,
            'callbackurl' => Config::get('axb.consulting_callbackurl', '')
        ];

        $axbPhone = $this->bindPhone($data);

        return [
            'axbPhone' => $axbPhone,
        ];
    }

    // 案如云-绑定号码
    private function bindPhone($params)
    {

        $response = $this->getAry()->bindConsultingPhone($params);
        $decodeContents = $response->getDecodeContentsOrThrow('获取号码失败');

        $axbPhone = $decodeContents['data']['telX'] ?? null;
        if (is_blank($axbPhone)) {
            throw new ValidateException('获取号码失败.');
        }

        return $axbPhone;
    }

    private function getAry(): Ary
    {
        return app(Ary::class);
    }
}
